<template>
  <div v-loading="loading" class="messageBox sub-main">
    <div v-if="flag">
      <div class="messageList">
        <div
          v-for="item of messageList"
          :key="item.noticeId"
          class="itemBox"
          @click="goToDetail(item)"
        >
          <div class="icon" :class="{color: item.noticeType === 3}">
            <icon-svg style="font-size: 36px" icon-class="user-notice-icon" />
            <div v-if="item.ifRead === 0" class="point" />
          </div>
          <div class="messageTitle">
            <h3
              v-if="item.noticeType === 1"
            >
              {{ item.noticeContent }}
            </h3>
            <h3
              v-if="[2,3].includes(item.noticeType)"
            >
              {{ item.noticeTitle }}
            </h3>
            <span>{{ item.createTime }}</span>
          </div>
          <el-button
            v-if="item.notice_type !== 3"
            class="delBtn"
            @click.stop="delNotice(item)"
          >
            <icon-svg style="font-size: 32px" icon-class="del" />
          </el-button>
        </div>
      </div>
      <div class="pageBox">
        <el-pagination
          v-if="messageList.length"
          background
          layout="prev, pager, next, jumper"
          :page-size="pageSize"
          :current-page="page"
          :total="total"
          @current-change="handleCurrentChange"
        />
        <el-button type="primary" size="mini" style="margin-left:10px;">确定</el-button>
      </div>
    </div>
    <div v-else class="nothing">
      <icon-svg style="width: 240px; height: 240px; margin: 40px;" icon-class="user-notice-nodata" />
      <p class="fs20 font-color-999">你还没有消息～</p>
      <!-- <router-link to="/">
          <el-button>去首页看看</el-button>
      </router-link> -->
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
// import {
//   getUserInfo
// } from '@/api/user/user.js'
// import {
//   getNoticeAll,
//   readNotice,
//   noticeDel
// } from '@/api/user/notice.js'
export default {
  name: 'Message',
  data() {
    return {
      messageList: [
        {
          'noticeId': 3415,
          'noticeTitle': 'fdsafd',
          'noticeType': 2,
          'jump': 0,
          'noticeContent': '<p>dasfasdffdasfdasf</p>',
          'createTime': '2023-02-17 11:39:44',
          'only': 0,
          'ifRead': 1
        },
        {
          'noticeId': 3412,
          'noticeTitle': '消息发送测试',
          'noticeType': 2,
          'jump': 0,
          'noticeContent': '<p>fdaffadsfadsfadsf</p>',
          'createTime': '2023-02-17 11:28:06',
          'only': 0,
          'ifRead': 1
        },
        {
          'noticeId': 3409,
          'noticeTitle': '前端用户站内信',
          'noticeType': 3,
          'jump': 0,
          'noticeContent': '<p>全员前端用户站内信推送。</p>',
          'createTime': '2023-01-31 15:47:35',
          'only': 0,
          'ifRead': 1
        },
        {
          'noticeId': 3408,
          'noticeTitle': '全员站内信',
          'noticeType': 3,
          'jump': 0,
          'noticeContent': '<p><span data-v-b59496e4="">这是一份来自平台管理员发给注册用户的站内信。</span></p>',
          'createTime': '2023-01-31 15:46:58',
          'only': 0,
          'ifRead': 1
        },
        {
          'noticeId': 3407,
          'noticeTitle': '全员消息-公告',
          'noticeType': 2,
          'jump': 0,
          'noticeContent': '<p><span data-v-b59496e4="">这是一份来自平台管理员发给全部用户的消息！</span></p>',
          'createTime': '2023-01-31 15:46:11',
          'only': 0,
          'ifRead': 1
        },
        {
          'noticeId': 3406,
          'noticeTitle': '发给注册用户的消息',
          'noticeType': 2,
          'jump': 0,
          'noticeContent': '<p>这是一份来自平台管理员发给注册用户的消息。</p>',
          'createTime': '2023-01-31 15:44:36',
          'only': 0,
          'ifRead': 1
        }
      ],
      page: 1,
      pageSize: 10,
      total: 1,
      flag: true,
      loading: false
    }
  },
  mounted() {
    this.getAll()
    this.getUserInfoData()
  },
  methods: {
    ...mapMutations({
      setUserInfo: 'SET_USERINFO'
    }),
    // 获取消息列表
    async getAll() {
      this.loading = true
      // const response = await getNoticeAll({
      //   page: this.page,
      //   pageSize: this.pageSize
      // })
      // const res = response.data
      const res = {
        'code': '200',
        'data': {
          'total': 6,
          'list': [
            {
              'noticeId': 3415,
              'noticeTitle': 'fdsafd',
              'noticeType': 2,
              'jump': 0,
              'noticeContent': '<p>dasfasdffdasfdasf</p>',
              'createTime': '2023-02-17 11:39:44',
              'only': 0,
              'ifRead': 1
            },
            {
              'noticeId': 3412,
              'noticeTitle': '消息发送测试',
              'noticeType': 2,
              'jump': 0,
              'noticeContent': '<p>fdaffadsfadsfadsf</p>',
              'createTime': '2023-02-17 11:28:06',
              'only': 0,
              'ifRead': 1
            },
            {
              'noticeId': 3409,
              'noticeTitle': '前端用户站内信',
              'noticeType': 3,
              'jump': 0,
              'noticeContent': '<p>全员前端用户站内信推送。</p>',
              'createTime': '2023-01-31 15:47:35',
              'only': 0,
              'ifRead': 1
            },
            {
              'noticeId': 3408,
              'noticeTitle': '全员站内信',
              'noticeType': 3,
              'jump': 0,
              'noticeContent': '<p><span data-v-b59496e4="">这是一份来自平台管理员发给注册用户的站内信。</span></p>',
              'createTime': '2023-01-31 15:46:58',
              'only': 0,
              'ifRead': 1
            },
            {
              'noticeId': 3407,
              'noticeTitle': '全员消息-公告',
              'noticeType': 2,
              'jump': 0,
              'noticeContent': '<p><span data-v-b59496e4="">这是一份来自平台管理员发给全部用户的消息！</span></p>',
              'createTime': '2023-01-31 15:46:11',
              'only': 0,
              'ifRead': 1
            },
            {
              'noticeId': 3406,
              'noticeTitle': '发给注册用户的消息',
              'noticeType': 2,
              'jump': 0,
              'noticeContent': '<p>这是一份来自平台管理员发给注册用户的消息。</p>',
              'createTime': '2023-01-31 15:44:36',
              'only': 0,
              'ifRead': 1
            }
          ]
        },
        'errorData': {},
        'message': '成功',
        'name': '',
        'describe': '',
        'json': ''
      }
      if (res.code === '200') {
        this.messageList = res.data.list
        this.total = res.data.total
        if (res.data.list.length) {
          this.flag = true
        } else {
          this.flag = false
        }
        this.loading = false
      } else {
        this.$message.warning(res.message)
      }
    },
    // 获取用户信息
    async getUserInfoData() {
      // const response = await getUserInfo()
      const response = {
        'code': '200',
        'data': {
          'buyerUserId': 2125,
          'name': 'MJ77465',
          'sex': '男',
          'birthday': '2022-12-07',
          'wechatOpenId': '',
          'wechatUnionId': '',
          'wechatName': '',
          'wechatNumber': '',
          'aliUserId': '',
          'phone': '18801263882',
          'password': '',
          'headImage': 'https://caf-mall.oss-cn-beijing.aliyuncs.com/test/2022-12-12/6393036dc3c944329a818d4c0e04622b_background-3017167_640.jpg',
          'state': 0,
          'ifBlack': 0,
          'remark': '',
          'token': '',
          'memberLevelId': 15,
          'memberLevelName': '普通会员',
          'growth': 0,
          'credit': 455,
          'terminal': 0,
          'nextLevelGrowth': 3800,
          'nextLevelName': '黑钻会员',
          'registerIp': '',
          'lastLoginIp': '',
          'channelCode': '',
          'createTime': '2022-12-01 17:32:40',
          'updateTime': '',
          'notRead': 0,
          'waitPayOrderCount': 0,
          'waitSendOrderCount': 0,
          'waitReceiveOrderCount': 0,
          'totalOrderCount': 0,
          'totalCredit': 455,
          'afterCount': 0,
          'collectCount': 0,
          'askAnswerCount': 0,
          'commentCount': 0,
          'visitCount': 9,
          'isBusiness': 0,
          'shopId': 0
        },
        'errorData': {},
        'message': '成功',
        'name': '',
        'describe': '',
        'json': ''
      };
      const res = response.data
      if (res.code === '200') {
        this.setUserInfo(res.data)
      } else {
        this.$message.warning(res.message)
      }
    },
    // 翻页
    handleCurrentChange(val) {
      this.page = val
      this.getAll()
    },
    // 查看详情
    async goToDetail(item) {
      // const response = await readNotice({ noticeId: item.noticeId })
      // const res = response.data
      const res = {
        'code': '200',
        'data': true,
        'errorData': {},
        'message': '成功',
        'name': '',
        'describe': '',
        'json': ''
      }
      if (res.code === '200') {
        this.noticeJump(item)
      } else {
        this.$message.warning(res.message)
      }
    },
    // 详情跳转
    noticeJump(item) {
      if (item.noticeType === 1) {
        if (item.jump === 0) {
          this.$router.push({
            path: '/notice/messageCenter/notificationDetails',
            query: {
              id: item.noticeId
            }
          })
        } else if (item.jump === 1) {
          this.$router.push({
            path: '/notice/messageCenter/notificationDetails',
            query: {
              id: item.noticeId
            }
          })
        } else if (item.jump === 2) {
          this.$router.push({
            path: '/orderDetail',
            query: {
              orderId: item.only,
              noticeId: item.noticeId
            }
          })
        }
      } else if (item.noticeType === 3 || item.noticeType === 2) {
        this.$router.push({
          path: '/notice/messageCenter/notificationDetails',
          query: {
            id: item.noticeId
          }
        })
      }
    },
    async delNotice(item) {
      if (!item.noticeId) {
        this.$message.error('选中消息异常！')
        return
      }
      // const response = await noticeDel({ noticeId: item.noticeId })
      // const res = response.data
      // if (res.code === '200') {
      //   this.$message.success('删除成功')
      //   this.getAll()
      // } else {
      //   this.$message.error(res.msg)
      // }
    }
  }
}
</script>

<style lang="scss" scoped>
$mainGlod: #c5aa7b; // 主题金
.messageBox {
  background: #FAFAFA;
  border: 1px solid #E5E5E5;
  padding: 25px;
  .pageBox {
    display: flex;
    justify-content: flex-end;
    .el-button{
      border-radius: 0;
    }
  }
  .messageList {
    .itemBox {
      height: 80px;
      padding: 10px 20px;
      margin-bottom: 30px;
      background-color: #FFF;
      display: flex;
      justify-content: space-between;
      align-items: center;
      transition: all 0.3s linear;
      cursor: pointer;
      &:hover{
        box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2);
        .delBtn{
          display: block;
        }
      }
      .icon {
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: #333333;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #FFFFFF;
        font-size: 26px;
        margin-right: 30px;
        position: relative;
        .point{
          width: 10px;
          height: 10px;
          border-radius: 5px;
          background-color: red;
          position: absolute;
          top: 10px;
          right: 10px;
        }
      }
      .color {
        background: #C5AA7B;
      }
      .messageTitle {
        flex: 1;
        h3 {
          color: #333333;
          font-size: 14px;
          margin-bottom: 20px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        span {
          color: #666666;
          font-size: 12px;
        }
      }
      .delBtn{
        display: none;
        animation: s 1.5s;
      }
      @keyframes s {
        from {
          opacity: 0;
        }
        to {
          opacity: 1;
        }
      }
    }
  }
}
.sub-main{
  min-height: 400px;
}
.nothing{
  width: 100%;
  text-align: center;
  min-height: 400px;
  p{
      margin-bottom: 20px;
  }
  .el-button{
      background-color: #FF7800;
      color: #FFFFFF;
      font-weight: normal;
      border-radius: 0;
  }
}
</style>
